package com.shop2cn.iapi.agentproducts.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.AddConditionRebateBean;
import com.shop2cn.iapi.agentproducts.parameter.EditConditionRebateBean;
import com.shop2cn.iapi.agentproducts.parameter.ListConditionRebateBean;
import com.shop2cn.iapi.agentproducts.service.ListConditionRebateCall;
import com.ymatou.iapi.godmch.parameter.createDepotBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.agentrebate.Ymt_ConditionRebate;
import com.ymttest.database.model.agentrebate.Ymt_ConditionRebateExample;
import com.ymttest.database.model.agentrebate.Ymt_ConditionRebateValue;
import com.ymttest.database.model.agentrebate.Ymt_ConditionRebateValueExample;
import com.ymttest.database.model.godmch.mch_depot_info;
/**
*返佣条件列表Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ListConditionRebate{
	private static ListConditionRebateBean listconditionrebateBean;
	private static ListConditionRebateCall listconditionrebateCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("返佣条件列表Test");
	}

	@Before
	public void caseUp() {
		listconditionrebateBean=new ListConditionRebateBean();
		listconditionrebateCall=new ListConditionRebateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(listconditionrebateCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	/**
	 * 查询列表，验证数据库
	 * @param bean
	 */
	private void checkConditionRebate(JsonObject json){
		json=json.getAsJsonObject("data");
		int num = json.get("totalsNum").getAsInt();
		JsonArray RebateList = json.getAsJsonArray("conditionRebateList");
		
		RebateList.forEach(b->{
			JsonObject Rebatejson = b.getAsJsonObject();
			int RebateId = Rebatejson.get("conditionRebateId").getAsInt();
			//使用id查询数据库
			Ymt_ConditionRebateExample example2=new Ymt_ConditionRebateExample();
			example2.createCriteria()
			.andConditionRebateIdEqualTo(RebateId);
			Ymt_ConditionRebate Rebate = SqlMapperFactory.getIntergratedProductMapper().selectYmtConditionRebateExample(example2).get(0);
			
			Logger.verifyEquals(Rebate.getUserId(),Rebatejson.get("userId").getAsInt(), "验证userId");
			Logger.verifyEquals(Rebate.getConditionType(),Rebatejson.get("weight").getAsInt(), "验证weight");
			Logger.verifyEquals(Rebate.getConditionType(),Rebatejson.get("weight").getAsInt(), "验证weight");
			//验证品牌
			String[] Brands = Rebate.getBrandIdList().split(",");
			if(Brands.length>1){
			for (int i = 0; i < Brands.length; i++) {
				String string = Brands[i];
				Logger.verifyEquals(string, Rebatejson.getAsJsonArray("brandInfos").get(0).getAsJsonObject().get("brandId").getAsString(), "验证品牌"+string);
			}
			Logger.verifyEquals(Brands.length, Rebatejson.getAsJsonArray("brandInfos").size(), "验证品牌个数");
			}
			
			//验证品类
			String[] categorys = Rebate.getFirstCategoryIdList().split(",");
			if(categorys.length>1){
			for (int i = 0; i < categorys.length; i++) {
				String string = categorys[i];
				Logger.verifyEquals(string, Rebatejson.getAsJsonArray("categoryInfos").get(0).getAsJsonObject().get("categoryId").getAsString(), "验证品类"+string);
			}
			Logger.verifyEquals(categorys.length, Rebatejson.getAsJsonArray("categoryInfos").size(), "验证品类个数");
			}
			
			
			//验证仓库
			String[] depots = Rebate.getDepotIdList().split(",");
			if(depots.length>1){
			for (int i = 0; i < depots.length; i++) {
				String string = depots[i];
				Logger.verifyEquals(string, Rebatejson.getAsJsonArray("depotInfos").get(0).getAsJsonObject().get("depotId").getAsString(), "验证仓库"+string);
			}
			Logger.verifyEquals(depots.length, Rebatejson.getAsJsonArray("depotInfos").size(), "验证仓库个数");
			}
			
			Logger.verifyEquals(Rebate.getAction(),Rebatejson.get("action").getAsInt(), "验证action");
		});
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListConditionRebate_001() {
		Logger.start(true,"查询返佣列表");
		try {
			Integer userId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			createDepotBean bean=GodmchManageCallService.createDepot(userId);
			List<mch_depot_info> selectDepotByDepotTitle = GodmchManageCallService.SelectDepotByDepotTitle(bean.getDepotTitle());
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			addAgentProductBean.setDepotId(selectDepotByDepotTitle.get(0).getDepotId());
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			AddConditionRebateBean initAddCondictionRebate = AddAgentProductCallService.InitAddCondictionRebate(addAgentProductBean,2);
			int RebateId = AddAgentProductCallService.AddCondictionRebate(initAddCondictionRebate);
			listconditionrebateBean.setUserId(userId);
			
			listconditionrebateCall.setData(listconditionrebateBean);
			listconditionrebateCall.callService();	
			Logger.verifyEquals("200", listconditionrebateCall.getString("code"), "验证Code");
			checkConditionRebate(listconditionrebateCall.getJsonObject());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListConditionRebate_002() {
		Logger.start(true,"查询返佣列表,已经删除的不显示");
		try {
			Integer userId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			AddConditionRebateBean initAddCondictionRebate = AddAgentProductCallService.InitAddCondictionRebate(addAgentProductBean);
			int RebateId = AddAgentProductCallService.AddCondictionRebate(initAddCondictionRebate);
			AddAgentProductCallService.DeleteConditionRebate(userId, RebateId);
			
			listconditionrebateBean.setUserId(userId);
			listconditionrebateCall.setData(listconditionrebateBean);
			listconditionrebateCall.callService();
			Logger.verifyEquals("200", listconditionrebateCall.getString("code"), "验证Code");
			Logger.verifyEquals(0, listconditionrebateCall.getJsonObject().getAsJsonObject("data").get("totalsNum").getAsInt(), "已经删除，查询数目为0");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListConditionRebate_003() {
		Logger.start(true,"查询返佣列表,禁用的显示禁用");
		try {
			Integer userId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			AddConditionRebateBean initAddCondictionRebate = AddAgentProductCallService.InitAddCondictionRebate(addAgentProductBean);
			int RebateId = AddAgentProductCallService.AddCondictionRebate(initAddCondictionRebate);
			AddAgentProductCallService.EnableConditionRebate(userId, RebateId, false);
			
			listconditionrebateBean.setUserId(userId);
			listconditionrebateCall.setData(listconditionrebateBean);
			listconditionrebateCall.callService();		
			Logger.verifyEquals("200", listconditionrebateCall.getString("code"), "验证Code");
			checkConditionRebate(listconditionrebateCall.getJsonObject());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListConditionRebate_004() {
		Logger.start(true,"查询返佣列表,禁用的再起启用，可以查询到");
		try {
			Integer userId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			AddConditionRebateBean initAddCondictionRebate = AddAgentProductCallService.InitAddCondictionRebate(addAgentProductBean);
			int RebateId = AddAgentProductCallService.AddCondictionRebate(initAddCondictionRebate);
			AddAgentProductCallService.EnableConditionRebate(userId, RebateId, false);
			AddAgentProductCallService.EnableConditionRebate(userId, RebateId, true);
			
			listconditionrebateBean.setUserId(userId);
			listconditionrebateCall.setData(listconditionrebateBean);
			listconditionrebateCall.callService();		
			Logger.verifyEquals("200", listconditionrebateCall.getString("code"), "验证Code");
			JsonObject jsonObject = listconditionrebateCall.getJsonObject();
			checkConditionRebate(listconditionrebateCall.getJsonObject());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListConditionRebate_005() {
		Logger.start(true,"多个返佣条件，进行查询");
		try {
			Integer userId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			AddAgentProductCallService.AddCondictionRebate(addAgentProductBean,1,10);
//			AddAgentProductCallService.AddCondictionRebate(addAgentProductBean,2,20);
//			AddAgentProductCallService.AddCondictionRebate(addAgentProductBean,3,30);
//			AddAgentProductCallService.AddCondictionRebate(addAgentProductBean,4,40);
//			AddAgentProductCallService.AddCondictionRebate(addAgentProductBean,5,30);
//			AddAgentProductCallService.AddCondictionRebate(addAgentProductBean,6,20);
//			AddAgentProductCallService.AddCondictionRebate(addAgentProductBean,7,10);
			
			listconditionrebateBean.setUserId(userId);
			listconditionrebateCall.setData(listconditionrebateBean);
			listconditionrebateCall.callService();		
			Logger.verifyEquals("200", listconditionrebateCall.getString("code"), "验证Code");
			checkConditionRebate(listconditionrebateCall.getJsonObject());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}